package com.github.want;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.Utils;
import com.github.camera.BasePictureActivity;
import com.github.camera.ImageAdapter;
import com.github.camera.PictureModel;
import com.github.camera.PictureSelector;
import com.github.camera.R;
import com.github.camera.utils.Constanst;
import com.tbruyelle.rxpermissions3.RxPermissions;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BasePictureActivity {
    private RecyclerView recyclerView;
    private RxPermissions rxPermissions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Utils.init(this.getApplication());
        recyclerView = findViewById(R.id.recyclerView);

        rxPermissions = new RxPermissions(this);

        Button cameraBtn = findViewById(R.id.cameraBtn);
        cameraBtn.setOnClickListener(v -> rxPermissions.request(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(granted -> {
            List<PictureModel> photolist = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                PictureModel pictureModel = new PictureModel();
                pictureModel.setName("第" + i + "张");
                pictureModel.setPath(null);
                photolist.add(pictureModel);
            }
            PictureSelector.build(MainActivity.this).cameraType(1).openLight(true).enableCrop(false).takePhotoModel(1).photoList(photolist).enableAlbum(true).builder(Constanst.START_PHOTO_REQUEST_CODE);
        }));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == Constanst.START_PHOTO_REQUEST_CODE) {
                List<PictureModel> photolist = (List<PictureModel>) data.getSerializableExtra(Constanst.CALL_BACK_PHOTO_LIST);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
                linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                recyclerView.setLayoutManager(linearLayoutManager);

                ImageAdapter imageAdapter = new ImageAdapter(this);
                imageAdapter.setDataList(photolist);
                recyclerView.setAdapter(imageAdapter);
            }
        }
    }
}
